package com.example.donghuihui.androidframe.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.telephony.TelephonyManager;

public class NetUtil {
    private static State mWifiState = null;
    private static State mMobileState = null;

    public static final int TYPE_WIFI = 1001;
    public static final int TYPE_MOBILE = 1002;

    public static boolean isConnected(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isAvailable()) {
            return false;
        } else {
            return true;
        }
    }

    public static int getNetType(Context context) {
        mWifiState = null;
        mMobileState = null;
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        manager.getActiveNetworkInfo();
        mWifiState = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .getState();
        mMobileState = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .getState();
        if (mWifiState != null && mMobileState != null
                && State.CONNECTED != mWifiState
                && State.CONNECTED == mMobileState) {
            return TYPE_MOBILE;
        } else if (mWifiState != null && mMobileState != null
                && State.CONNECTED != mWifiState
                && State.CONNECTED != mMobileState) {
            return 0;
        } else if (mWifiState != null && State.CONNECTED == mWifiState) {
            return TYPE_WIFI;
        }
        return 0;
    }

    /**
     * 获取网络类型
     *
     * @param context
     */
    public static String getActiveNetworkType(Context context) {
        ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cManager.getActiveNetworkInfo();
        if (info == null || !info.isAvailable()) {
            return "disconnect";
        }
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            return "WIFI";
        }
        if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            int sub = info.getSubtype();
            String typeStr;
            switch (sub) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    typeStr = "2G";
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    typeStr = "3G";
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:
                    typeStr = "4G";
                    break;
                case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                    typeStr = "unknown";
                    break;
                default:
                    typeStr = "unknown";
                    break;
            }
            return typeStr + "-" + info.getSubtypeName();
        }
        return "unknown";
    }

}
